package cn.wangchao.basic.controller;

import cn.wangchao.basic.dto.RegisterDto;
import cn.wangchao.basic.exception.BusinessException;
import cn.wangchao.basic.service.IVerifyCodeService;
import cn.wangchao.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult image(@PathVariable("imageCodeKey") String key){
        try {
        String code = verifyCodeService.imageCode(key);
        return new AjaxResult().setResultObj(code);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterDto dto){
        try {
            verifyCodeService.smsCode(dto);
            return AjaxResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        }

    }


}
